package com.cirkana.viewer.share;

import winterwell.jtwitter.OAuthSignpostClient;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.cirkana.CirkanaActivity;
import com.cirkana.DateUtil;
import com.cirkana.serial.SerializationException;
import com.cirkana.viewer.R;

public class ShareActivity extends CirkanaActivity {

	private static final String JTWITTER_OAUTH_KEY = "OEfL8LvCMeVLoITt6Nn9g";
	private static final String JTWITTER_OAUTH_SECRET = "qoAYqiQtCn2KR0CyZGKbwPqarGH2dbsRUtpBIGstw9U";

	private static String requestToken = "284095635-A60nkEBwax1EWdsECFaANIqaCVI19KzUQGfWRPS9";
	private static String requestSecret = "I1RC8kR1WYvMEiLrO0A0ThFqf0j2lWyZvDRFy2NNac";

	private static String twitterUser = "bernatfabregat";

	private static boolean hasAccessTokens() {
		return (requestToken != null && requestSecret != null);
	}

	public static final int SHARE_CHANNEL_NOTSET = 0;
	public static final int SHARE_CHANNEL_TWITTER = 1;
	public static final int SHARE_CHANNEL_EMAIL = 2;

	public static final int RESULT_SHARE_OK = 0;

	private static final int REQUEST_AUTHORINGTWITTER = 1;

	private EditText messageEditText = null;
	private Button sendButton = null;

	private SharePassData data;
	private OAuthSignpostClient oauthClient = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.share);

		this.messageEditText = (EditText) findViewById(R.id.share_message);
		this.sendButton = (Button) findViewById(R.id.share_send);

		this.data = (SharePassData) getRequest(SharePassData.class);

		switch (data.getShareType()) {
		case SHARE_CHANNEL_TWITTER:
			setTitle(getText(R.string.share_on) + " "
					+ getString(R.string.twitter));
		}
		this.messageEditText.getText().append(
				" at " + DateUtil.getPresentationDate());

		this.sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!hasAccessTokens()) {
					try {
						oauthClient = new OAuthSignpostClient(
								JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET,
								"oob");
						oauthClient.setName(twitterUser);
						startActivityForResult(AuthoringTwitterActivity.class,
								REQUEST_AUTHORINGTWITTER,
								new AuthoringTwitterPassData(oauthClient
										.authorizeUrl().toString(), null));
					} catch (Exception e) {
						showErrorMessage(e);
					}
				} else {
					try {
						oauthClient = new OAuthSignpostClient(
								JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET,
								requestToken, requestSecret);
					} catch (Exception e) {
						showErrorMessage(e);
					}
					sendTwitterMessage();
				}
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REQUEST_AUTHORINGTWITTER:
			switch (resultCode) {
			case AuthoringTwitterActivity.RESULT_AUTHORINGTWITTER_OK:
				try {
					AuthoringTwitterPassData result = (AuthoringTwitterPassData) getResponse(
							data, AuthoringTwitterPassData.class);
					oauthClient.setAuthorizationCode(result.getPin());
					String[] accessToken = oauthClient.getAccessToken();
					requestToken = accessToken[0];
					requestSecret = accessToken[1];
					sendTwitterMessage();
				} catch (SerializationException e) {
					showErrorMessage(e);
				}
				break;
			}
			break;
		}

	}

	private void sendTwitterMessage() {
		try {
//			Twitter twitter = new Twitter(twitterUser, oauthClient);
//			twitter.setStatus(messageEditText.getText().toString());
			setResponse(RESULT_SHARE_OK, data);
			finish();
		} catch (Exception e) {
			showErrorMessage(e);
		}
	}
}
